import Cookies from 'js-cookie'
import { applicationConfiguration } from '@/api/abp'
import qiankunActions from '@/shared/actions'// 注入qiankun通讯组件

const __qiankun__ = window.__POWERED_BY_QIANKUN__

const state = {
  sidebar: {
    // opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    opened: 1,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  abpConfig: null,
  centralConfig: null

}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  SET_CENTRALCONFIG: (state, centralConfig) => {
    state.centralConfig = centralConfig
  },
  SET_ABPCONFIG: (state, abpConfig) => {
    state.abpConfig = abpConfig
  },
  RESET_ABPCONFIG: state => {
    state.abpConfig = null
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  applicationConfiguration({ commit }) {
    return new Promise((resolve, reject) => {
      if (__qiankun__ && state.centralConfig) {
        // console.log('已有state.abpConfig', state.abpConfig)
        resolve(state.centralConfig)
      } else {
        applicationConfiguration()
          .then(response => {
            commit('SET_CENTRALCONFIG', response)
            resolve(response)
            // qiankun 通信
            qiankunActions.setGlobalState({ abpConfig: response })
          })
          .catch(error => {
            reject(error)
          })
      }
      // if (__qiankun__ && state.abpConfig) {
      //   // console.log('已有state.abpConfig', state.abpConfig)
      //   resolve(state.abpConfig)
      // } else {
      //   applicationConfiguration()
      //     .then(response => {
      //       console.log('applicationConfiguration', response?.currentUser.isAuthenticated)
      //       commit('SET_ABPCONFIG', response)
      //       resolve(response)
      //       // qiankun 通信
      //       qiankunActions.setGlobalState({ abpConfig: response })
      //     })
      //     .catch(error => {
      //       reject(error)
      //     })
      // }
    })
  },
  resetApplicationConfiguration({ commit }) {
    commit('SET_ABPCONFIG', null)
  },
  setAbplicationConfiguration({ commit }, abpConfig) {
    // console.log('setAbplicationConfiguration 进行存储', abpConfig)
    commit('SET_ABPCONFIG', abpConfig)
    return new Promise((resolve, reject) => {
      if (!abpConfig) {
        reject('abpConfig is null')
      } else {
        resolve()
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
